package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    public R saveDishWithFlover(@RequestBody DishDto dishDto){

        dishService.saveFloverAndDish(dishDto);
        //清理某个分类下面的菜品缓存数据
        String key = "dish_" + dishDto.getCategoryId() + "_1";
        redisTemplate.delete(key);
        return R.success(null);
    }

    @GetMapping("/page")
    public R pageDishManage(int page,int pageSize,String name){
        /*
        1.构造分页构造器
        2.进行多表分页查询,此时需要BeanUtils这个工具类,进行复制
        3.把dish表中菜品分类id与category菜品名称进行匹配,使dto里面有这2个数据
        4.重新放回新的分页构造器中
        5.执行分页查询
         */
        Page<DishDto> dishDtoPageInfo=dishService.pageDishManage(page,pageSize,name);
        return R.success(dishDtoPageInfo);
    }

    @GetMapping("/{id}")
    public R dishManageDataShow(@PathVariable Long id){
        /*
            A. 根据ID查询菜品的基本信息
            B. 根据菜品的ID查询菜品口味列表数据
            C. 组装数据并返回
         */
        DishDto dishDto=dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R updateDish(@RequestBody DishDto dishDto){
        /*
        对更新菜品进行优化
         */
        dishService.updateWithFlavor(dishDto);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return R.success(null);
    }


    @GetMapping("/list")
    public R dishLists(Dish dish){
        /*
        进行优化，把热点的菜品数据存储到redis中，如果redis中有就在redis中取出，没有的话就在数据库中取出，数据在返回到redis中
         */
        List<DishDto> dishDtos=null;
        String key="dish_"+dish.getCategoryId()+"_"+dish.getStatus();
        dishDtos = (List<DishDto>) redisTemplate.opsForValue().get(key);

        if (dishDtos==null){
            dishDtos=dishService.dishList(dish);//在数据库中查询
            redisTemplate.opsForValue().set(key, dishDtos,60, TimeUnit.MINUTES);//并存储到redis中
        }
        return R.success(dishDtos);
    }

    @PostMapping("/status/{id}")
    public R updateStatus(@PathVariable Long id,@RequestParam List<Long> ids){
        /*
        1.获取dishid
        2.根据dishid进行修改状态
         */
        dishService.updateDish(id,ids);
        return R.success(null);
    }
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        /*
        0.先判断当前状态是否为停售状态，如果为停售状态则可以删除，
        如果不为则不能删除，抛出一个异常，先停售在删除
        1.根据id进行删除
         */
        dishService.delete(ids);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return R.success(null);
    }

}
